package com.java.demo.demo;

import lombok.Data;

import java.util.concurrent.TimeUnit;

public class Demo7 {

    public static void main(String[] args) {
        System.out.println(RedisKeyEnum.DEMO.getRedisKey("222", "333", "444"));
        System.out.println(RedisValue.builder(111));
    }

    public <T> void demo(RedisKeyEnum redisKey, RedisValue<T> redisValue, String... suffix) {
        String key = redisKey.getRedisKey(suffix);
    }

    public interface IRedisKey {

    }

    public static abstract class AbstractRedisKey {

        /**
         * 分割字符，默认[:]，使用:可用于rdm分组查看
         */
        String KEY_SPLIT_CHAR = ":";

        abstract String projectKey();

        abstract String redisKey();

        protected String getRedisKey(String... suffix) {
            StringBuilder sb = new StringBuilder();
            sb.append(projectKey());
            sb.append(KEY_SPLIT_CHAR);
            sb.append(redisKey());
            if (suffix != null) {
                for (String s : suffix) {
                    sb.append(KEY_SPLIT_CHAR);
                    sb.append(s);
                }
            }
            return sb.toString();
        }
    }


    @Data
    public static class RedisValue<T> {

        private T value;

        private long expireTime;

        private TimeUnit timeUnit;

        public static <T> RedisValue<T> builder(T value) {
            RedisValue<T> redisValue = new RedisValue<>();
            redisValue.setValue(value);
            redisValue.setExpireTime(-1L);
            redisValue.setTimeUnit(TimeUnit.SECONDS);
            return redisValue;
        }
    }

    public enum RedisKeyEnum {

        DEMO("111");

        /**
         * 主数据系统标识
         */
        private static final String KEY_PREFIX = "airCourse";
        /**
         * 分割字符，默认[:]，使用:可用于rdm分组查看
         */
        private static final String KEY_SPLIT_CHAR = ":";

        private final String redisKey;

        RedisKeyEnum(String redisKey) {
            this.redisKey = redisKey;
        }

        public String getRedisKey() {
            return keyBuilder(this.redisKey, (String[]) null);
        }

        public String getRedisKey(String... suffix) {
            return keyBuilder(this.redisKey, suffix);
        }

        private String keyBuilder(String prefix, String... args) {
            StringBuilder key = new StringBuilder(KEY_PREFIX);
            key.append(KEY_SPLIT_CHAR).append(prefix);
            if (null != args) {
                for (String arg : args) {
                    key.append(KEY_SPLIT_CHAR).append(arg);
                }
            }
            return key.toString();
        }
    }
}